{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [handcalcs](https://github.com/connorferster/handcalcs)\n",
    "Python library for converting Python calculations into rendered latex.\n",
    "\n",
    "## [教程](https://mp.weixin.qq.com/s/LumDQL9VKTHWK3RkOhKFcg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Python语法基础上，将计算公式呈现为Latex格式，只需要加一行代码`%%render`\n",
    "- 在函数中用这个工具插件，那就再加一行`@handcalc()`，但要注意`return locals()`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- `#short`     #公式同行显示\n",
    "- `#long`      #公式换行显示  \n",
    "- `#parameters`  #参数同行显示\n",
    "- `#symbolic`   #去掉计算结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import handcalcs.render\n",
    "from math import sqrt, sin, asin, pi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[\n",
       "\\begin{aligned}\n",
       "a &= 2 &b &= 3 &c &= 5.0\\\\\n",
       "\\end{aligned}\n",
       "\\]"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%render\n",
    "#parameters\n",
    "a = 2\n",
    "b = 3\n",
    "c = 2*a + b/3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[\n",
       "\\begin{aligned}\n",
       "d &= 4.379\\;\n",
       "\\\\[10pt]\n",
       "f &= \\frac{ d }{ a } + b = \\frac{ 4.379 }{ 2 } + 3 &= 3.5\n",
       "\\\\[10pt]\n",
       "g &= d \\cdot \\frac{ f }{ a } = 4.379 \\cdot \\frac{ 3.5 }{ 2 } &= 1.75\n",
       "\\\\[10pt]\n",
       "d &= \\sqrt{ \\left( \\frac{ a }{ b } \\right) } + \\arcsin{ \\left( \\sin{ \\left( \\frac{ b }{ c } \\right) } \\right) } + \\left( \\frac{ a }{ b } \\right) ^{ \\left( 0.5 \\right) } + \\sqrt{ \\left( \\frac{ a \\cdot b + b \\cdot c }{ \\left( b \\right) ^{ 2 } } \\right) } + \\sin{ \\left( \\frac{ a }{ b } \\right) } \\\\&= \\sqrt{ \\left( \\frac{ 2 }{ 3 } \\right) } + \\arcsin{ \\left( \\sin{ \\left( \\frac{ 3 }{ 5.0 } \\right) } \\right) } + \\left( \\frac{ 2 }{ 3 } \\right) ^{ \\left( 0.5 \\right) } + \\sqrt{ \\left( \\frac{ 2 \\cdot 3 + 3 \\cdot 5.0 }{ \\left( 3 \\right) ^{ 2 } } \\right) } + \\sin{ \\left( \\frac{ 2 }{ 3 } \\right) } \\\\&= 4.379\\\\\n",
       "\\end{aligned}\n",
       "\\]"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%render\n",
    "#sort\n",
    "d = 1\n",
    "f = d / a + b\n",
    "g = d * f / a\n",
    "d = sqrt(a/b) + asin(sin(b/c)) + (a/b)**(0.5) + sqrt((a*b+b*c)/(b**2)) + sin(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [],
   "source": [
    "import handcalcs.render\n",
    "from math import sqrt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[\n",
       "\\begin{aligned}\n",
       "a &= 23.2\\;\n",
       "\\\\[10pt]\n",
       "b &= 9.4\\;\n",
       "\\\\[10pt]\n",
       "c &= \\sqrt{ \\left( \\frac{ 2 \\cdot a + \\left( b \\right) ^{ 2 } }{ 3 \\cdot a } \\right) }\\;\n",
       "\\end{aligned}\n",
       "\\]"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%render\n",
    "#symbolic\n",
    "a = 23.2\n",
    "b = 9.4\n",
    "#c = (3*a)/sqrt(2*a+b**2)\n",
    "c = sqrt((2*a+b**2)/(3*a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "#@handcalc()\n",
    "def my_calc(x, y, z):\n",
    "    a = 2*x / y\n",
    "    b = 3*a\n",
    "    c = (a + b) / z\n",
    "    return locals()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[\n",
       "\\begin{aligned}\n",
       "\\end{aligned}\n",
       "\\]"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%render\n",
    "#my_calc(1,1,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "import handcalcs.render\n",
    "from scipy import integrate\n",
    "\n",
    "#%%render\n",
    "#parameters\n",
    "a = 2.4\n",
    "b = 9.2\n",
    "\n",
    "def F(x):\n",
    "    return x**2 + 10*x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[\n",
       "\\begin{aligned}\n",
       "g &= \\int_{ a } ^ { b } \\left( x \\right) ^{ 2 } + 10 \\cdot x \\; dx \\\\&= \\int_{ 2.4 } ^ { 9.2 } \\left( x \\right) ^{ 2 } + 10 \\cdot x \\; dx \\\\&= (649.3546666666665, 7.209285020811268e-12)\\\\\n",
       "\\end{aligned}\n",
       "\\]"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%render\n",
    "#long\n",
    "g = integrate.quad(F, a, b)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
